const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
// const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
// const before = require('../test')
const { getEntryFile } = require("./getEntryFile");
const baseUrl = process.env.NODE_ENV === "department" ? `` : "/static";
console.log('sss',path.resolve(__dirname, "../example/index.jsx"))
module.exports = {
  // @todo 变成自动读取
  entry: {
    // entry: path.resolve(__dirname, '../src/entry.tsx'),
     main: path.resolve(__dirname, "../example/index.jsx"),
    detail: path.resolve(__dirname, "../example/detail.tsx"),
    dashboard: path.resolve(__dirname, "../example/dashboard.tsx"),
    react16: path.resolve(__dirname, "../example/react16.jsx"),
    react17: path.resolve(__dirname, "../example/react17.jsx"),
    home: path.resolve(__dirname, "../example/home.tsx"),
    form: path.resolve(__dirname, "../example/form.tsx"),
    login: path.resolve(__dirname, "../example/login.jsx"),
    // // @todo 调用配置项，读取 pages
    //  ...getEntryFile(),
  },
  output: {
    filename: "[name].js",
    path: path.resolve(__dirname, "../dist"),
    clean: true,
    publicPath: "",
    libraryTarget: "umd",
  },
  externals: {
    react: "react",
    "react-dom": "react-dom",
    "react-dom/server": "react-dom/server",
    moment: "moment",
    antd: 'antd',
    lodash: "lodash",
  },
  // stats: "errors-warnings",
  resolve: {
    extensions: [".ts", ".tsx", ".js", ".jsx"],
    alias: {
      "@web-design/ui": path.resolve(__dirname, "../package/ui/src"),
      "@web-design/account": path.resolve(__dirname, "../package/account/src"),
      "@web-design/micro-app": path.resolve(__dirname, "../package/micro-app/src"),
      "@web-design/micro-app-next": path.resolve(__dirname, "../package/micro-app-next/src"),
      "@web-design/layout": path.resolve(__dirname, "../package/layout/src"),
      "@web-design/web-hook": path.resolve(__dirname, "../package/web-hook/src"),
    },
  },
  module: {
    rules: [
      {
        test: /\.(j|t)sx?$/,
        exclude: /node_modules/,
        loader:'ts-loader',
        // loader: "babel-loader",
        // use: {
        //   loader: 'babel-loader',
        //   options: {
        //     presets: [
        //       ['@babel/preset-react', { targets: "defaults" }],
        //       '@babel/preset-typescript'
        //     ],
        //     plugins: ['@babel/plugin-syntax-jsx']
        //   }
        // }
        // options: {
        //   transpileOnly: true,
        //   getCustomTransformers: (program) => {

        //     // const transformer = tsTransformPaths(program);
        //     return {
        //       before: [before()]
        //     }
        //   }
        // },
      },
      {
        test: /\.less$/,
        // exclude: /node_modules/,
        use: [
          {
            loader: "style-loader",
          },
          {
            loader: "css-loader",
          },
          {
            loader: "less-loader",
            options: {
              lessOptions: { javascriptEnabled: true },
            },
          },
        ],
      },
      {
        test: /\.(jpg|png|gif|bmp|jpeg)$/,
        use: [
          {
            loader: "url-loader",
            options: {
              outputPath: "/static/resource/img/",
              limit: 8192,
            },
          },
        ],
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.svg$/,
        exclude: /node_modules/,
        loader: "@svgr/webpack",
      },
    ],
  },
  plugins: [
    new webpack.DefinePlugin({
      "process.env": JSON.stringify({
        ...process.env,
      }),
    }),
    new HtmlWebpackPlugin({
      filename: path.resolve(__dirname, "../dist/index.html"),
      template: path.resolve(__dirname, `../public/index.html`),
      templateParameters: {
        entryPath: `${baseUrl}/entry.js?v=${Number(new Date())}`,
      },
      inject: false, // @todo 'body',
      // templateContent: {}, // function() {}
    }),
    // new ForkTsCheckerWebpackPlugin({
    //   eslint: {
    //     files: './src/**/*.{ts,tsx,js,jsx}',
    //   },
    // }),
  ],
  optimization: {
    usedExports: false,
  },
};
